<?php

  require_once "JooS/Helper/Broker.php";

  final class JooS_Browser {
    private static $_engines = array();

    public static function addEngine($name) {
      array_push(self::$_engines, $name);
    }

    public static function getBrowser($ua) {
      JooS_Helper_Broker::helperBroker()->Browser_InitHelper();

      $ret = null;
      foreach (self::$_engines as $engine) {
        require_once 'JooS/Browser/' . $engine . ".php";

        $nClass = "JooS_Browser_" . $engine;
        if (class_exists($nClass)) {
          $rClass = new ReflectionClass($nClass);

          if ($rClass->getParentClass()->getName() == "JooS_Browser_Abstract") {
            if ($ret = $rClass->newInstance($ua)->getInfo())
              break;
          }
          else
            self::throwException("Class $nClass must extend JooS_Browser_Abstract");
        }
        else
          self::throwException("Class $nClass not found");
      }

      return $ret;
    }

    private static function throwException($message) {
      require_once 'JooS/Browser/Exception.php';

      throw new JooS_Browser_Exception($message);
    }
  }

?>